﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using TA.Win.Utils.Design;
using System.Drawing.Design;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Specifies how a property of an object is JSON serialized and deserialized.
	/// </summary>
	[ParseChildren(true, "Children")]
	public class PropertyPair
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public PropertyPair()
		{
			this.ObjectProperty = "";
			this.JSONProperty = "";
		}
		/// <summary>
		/// Ctor.
		/// </summary>
		/// <param name="objectProperty"></param>
		/// <param name="jsonProperty"></param>
		public PropertyPair(string objectProperty, string jsonProperty)
		{
			this.ObjectProperty = objectProperty;
			this.JSONProperty = jsonProperty;
		}
		#endregion
		#region EVENTS
		public event EventHandler<PropertyEventArgs> Resolve;
		#endregion
		#region PROPS
		/// <summary>
		/// Gets or sets the name of the property on the server-side object.
		/// </summary>
		[PersistenceMode(PersistenceMode.Attribute)]
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string ObjectProperty
		{
			get { return this._objectProperty; }
			set
			{
				this._objectProperty = value;
				//if (string.IsNullOrEmpty(this.JSONProperty))
				//{
				//  if (string.IsNullOrEmpty(value))
				//    this.JSONProperty = "";
				//  else
				//    this.JSONProperty = value.Substring(0, 1);
				//}
			}
		}
		/// <summary>
		/// Gets or sets the name of the property in JSON input or output.
		/// </summary>
		[PersistenceMode(PersistenceMode.Attribute)]
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string JSONProperty { get; set; }
		/// <summary>
		/// Gets the mutable collection of <see cref="PropertyPair"/>s which are used to serialize the child object's properties.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		[NotifyParentProperty(true)]
		[Editor(typeof(NestedCollectionEditor), typeof(UITypeEditor))]
		public List<PropertyPair> Children { get { return this._subProperties; } }
		/// <summary>
		/// Gets or sets an array of <see cref="PropertyPair"/>s which are used to serialize the child object's properties.
		/// </summary>
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[NotifyParentProperty(true)]
		public PropertyPair[] ChildrenArray
		{
			get
			{
				return this._subProperties.ToArray();
			}
			set
			{
				this._subProperties.Clear();
				this._subProperties.AddRange(value);
			}
		}
		#endregion
		#region METHODS
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		List<PropertyPair> _subProperties = new List<PropertyPair>();
		string _objectProperty;
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		#region INIT
		#endregion
		#region DATA
		protected internal bool TryResolve(object entity, out object result)
		{
			if (this.Resolve != null)
			{
				PropertyEventArgs ea = new PropertyEventArgs(entity);
				this.Resolve(this, ea);
				result = ea.Result;
				return true;
			}
			result = null;
			return false;
		}
		#endregion
		#region HANDLERS
		#endregion
		#endregion
		#endregion


	}
}
